package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.services.business.dto.request.create.VehicleCreateBizDto;
import com.molichuxing.services.infrastructure.bean.CarDetailBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.CarDetailMapper;
import com.molichuxing.services.infrastructure.dto.request.modify.CarDetailModifyDto;
import com.molichuxing.services.infrastructure.dto.response.CarDetailDto;
import com.molichuxing.services.infrastructure.service.CarDetailService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 车辆详情表 服务实现类
import com.alibaba.fastjson.JSON;
import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.services.infrastructure.bean.CarBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.CarDetailMapper;
import com.molichuxing.services.infrastructure.dao.CarMapper;
import com.molichuxing.services.infrastructure.dto.request.modify.CarDetailModifyDto;
import com.molichuxing.services.infrastructure.dto.response.CarDetailDto;
import com.molichuxing.services.infrastructure.dto.response.CarDto;
import com.molichuxing.services.infrastructure.service.CarDetailService;
import com.molichuxing.services.infrastructure.service.CarService;
import com.molichuxing.services.property.CarLicencePlateStatusEnum;
import com.molichuxing.services.property.CarTabEnum;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 车辆表 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-08-15
 */
@Service("carDetailService")
public class CarDetailServiceImpl implements CarDetailService {

  private static final Logger log = Logger.getLogger(CarDetailServiceImpl.class);

  @Resource
  private CarDetailMapper carDetailMapper;



  @Override
  public CarDetailDto getCarDetailById(Integer carId) {
    return Convert.toCarDetailDto(carDetailMapper.getById(carId));
  }

  @Override
  public Integer createCarDetail(VehicleCreateBizDto createBizDto) {
    CarDetailBean carDetailBean = new CarDetailBean();
    carDetailBean.setId(createBizDto.getCarId());
    carDetailBean.setProduceYear(createBizDto.getProductYear());
    carDetailBean.setCertificateCode(createBizDto.getCertificateCode());
    carDetailBean.setEngineCode(createBizDto.getEngineCode());
    return carDetailMapper.createCarDetail(carDetailBean);
  }

  @Override
  public Boolean modifyCarDetail(CarDetailModifyDto modifyDto) {
    int result = carDetailMapper.modifyCarDetailById(Convert.toCarDetailBean(modifyDto));
    if(result > 0) {
      return true;
    }
    return false;
  }

  /**
   * 根据车辆id查询车辆信息
   *
   * @param carIdList
   * @return
   */
  @Override
  public Map<Integer, CarDetailDto> getCarDetailMapByIds(List<Integer> carIdList) {
    if(null==carIdList || carIdList.size() < 1){
      return null;
    }

    List<CarDetailBean> carDetailList = carDetailMapper.getByIds(carIdList);
    if(null==carDetailList || carDetailList.size() < 1){
      return null;
    }
    List<CarDetailDto> carDetailDtoList = Convert.toCarDetailDto(carDetailList);

    return carDetailDtoList.stream().collect(Collectors.toMap(CarDetailDto::getId, a -> a, (k1, k2) -> k1));
  }

  @Override
  public Map<Integer, CarDetailDto> getCarDetailMap() {
    List<CarDetailBean> carDetailList = carDetailMapper.getCarDetailList();
    List<CarDetailDto> carDetailDtoList = Convert.toCarDetailDto(carDetailList);
    return carDetailDtoList.stream().collect(Collectors.toMap(CarDetailDto::getId, a -> a, (k1, k2) -> k1));
  }

  @Override
  public CarDetailDto getCarDetailByEngineCode(String engineCode) {
    return Convert.toCarDetailDto(carDetailMapper.getCarDetailByEngineCode(engineCode));
  }

}
